package com.sojpt.boot.controller.business;

import java.util.*;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sojpt.boot.controller.business.validator.LkyMajorAddValidator;
import com.sojpt.boot.controller.business.validator.LkyMajorUpdateValidator;
import com.sojpt.boot.intercepter.admin.AdminLoginInterceptor;
import com.sojpt.boot.intercepter.admin.AdminPopedomInterceptor;
import com.sojpt.boot.ui.iview.IViewController;
import com.sojpt.kit.SoJLogKit;
import com.sojpt.model.DicPopedom;
import com.sojpt.model.LkyClass;
import com.sojpt.model.LkyMajor;
import com.sojpt.util.ResultUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author MW
 */
@RestController
@RequestMapping("/admin/major")

public class LkyMajorAdminController extends IViewController {


    @GetMapping("/list")
    public void list() {
        LkyMajor lkyMajor = getModel(LkyMajor.class, "lkyMajor");
        Map<String,String> map =new HashMap<>();
        map.put("name",lkyMajor.getName());
        map.put("majorNumber",lkyMajor.getMajorNumber());
        map.put("coid",lkyMajor.getCoid());
        SqlPara sqlPara = Db.getSqlPara("lky_major.majorcoller",map);
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }

    @Before(LkyMajorAddValidator.class)
    @PostMapping("/save")
    public void save() {
        LkyMajor lkyMajor = getModel(LkyMajor.class, "lkyMajor");
        lkyMajor.setId(UUID.randomUUID().toString().replace("-", ""));
        lkyMajor.setCreationTime(new Date());
        lkyMajor.setUpdateTime(new Date());
        if (lkyMajor.save()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    @Before(LkyMajorUpdateValidator.class)
    @PostMapping("/update")
    public void update() {
        LkyMajor lkyMajor = getModel(LkyMajor.class, "lkyMajor");
        lkyMajor.setUpdateTime(new Date());
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/major/update");
        if (lkyMajor.update()) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("修改成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("修改失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("/delete")
    public void delete() {
        LkyMajor lkyMajor = getModel(LkyMajor.class, "lkyMajor");
        String id = lkyMajor.getId();
        String mid = Db.queryStr("select count(*) from lky_class where mid = ?", id);
        int a = Integer.parseInt(mid);
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/major/delete");
        if (a > 0) {
            renderJson(false);
            dicPopedom.setPopedomName("删除失败");
        } else {
            if (LkyMajor.dao.deleteById(lkyMajor.getId())) {
                renderJson(true);
                dicPopedom.setPopedomName("删除成功");
            } else {
                renderJson(false);
                dicPopedom.setPopedomName("删除失败");
            }
        }

        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("/batchDelete")
    public void batchDelete() {
        String[] ids = getPara("ids").split(",");
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/major/batchDelete");
        SqlPara sqlPara = Db.getSqlPara("lky_major.deleteByIds", Kv.by("idList", Arrays.asList(ids)));
        if (Db.update(sqlPara) > 0) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("批量删除成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("批量删除失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    //通过学院传回得id查找专业
    @PostMapping("/queryAllM")
    public void queryAllM() {
        String coid = getPara("coid");
        SqlPara sqlPara = Db.getSqlPara("lky_major.findmajorcoller", Kv.by("coid", coid));
        List<LkyMajor> lkyMajors = LkyMajor.dao.find(sqlPara);
        renderJson(ResultUtil.success(lkyMajors));
    }

}